package com.xyc.www.gulimall.test.sso.client.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    private String ssoServer;

    /**
     * 无需登录就可访问
     *
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    /**
     * 感知这次是在ssoserver
     *
     * @param model
     * @param session
     * @param token   只要去ssoserver登录成功跳回来就会带上
     * @return
     */
    @GetMapping("/employees")
    public String employees(Model model,
                            HttpSession session,
                            @RequestParam(value = "token", required = false) String token) {
        if (!StringUtils.isEmpty(token)) {
            //只要去ssoserver登录成功跳回来就会带上
            // TODO 1、去ssoserver获取当前token真正对应的用户信息
            session.setAttribute("loginUser", "张三");
        }

        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            //没登录，跳转到登录服务器
            return "redirect:" + ssoServer + "?redirect_url=http://client1.com:40001/employees";
        } else {
            List<String> emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");

            model.addAttribute("emps", emps);
            return "list";
        }
    }
}
